package org.smarthings;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

public class Utils {
	public static String injectHost = null;

	public static Properties getConfiguration(Object o, String filename) {
		Properties configuration = new Properties();
		try {
			InputStream stream = o.getClass().getClassLoader().getResourceAsStream(filename);
			if (stream == null) {
				stream = new FileInputStream(new java.io.File("transport.properties"));
			}
			if (stream != null) {
				configuration.load(stream);
			}
		} catch (Exception e) {
			// ignore e.printStackTrace();
		}
		if (injectHost != null) {
			configuration.setProperty("host", injectHost);
		}
		return configuration;

	}

	public static Message httpGET(String uri, String contentType) {
		Message message = null;
		HttpURLConnection connection = null;
		try {
			// Open Connection
			connection = (HttpURLConnection) new URL(uri).openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Accept", contentType);
			connection.setDoInput(true);

			// Get Response
			InputStream is = connection.getInputStream();
			byte[] payload = IOUtils.toByteArray(is);
			is.close();

			message = new Message(payload, MessageBroker.PROTOCOL, connection.getURL().getPath(), contentType,
					contentType, connection.getResponseCode(), null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}

		return message;
	}

	public static Response httpPOST(String uri, Message message) {
		HttpURLConnection connection = null;
		Response response = null;

		try {
			String method = message.getMethod() != null ? message.getMethod() : "POST";

			// Open Connection
			connection = (HttpURLConnection) new URL(uri).openConnection();
			connection.setRequestMethod(method);
			connection.setRequestProperty("Content-Type", message.getContentType());
			connection.setRequestProperty("Accept", message.getAccept());
			connection.setDoOutput(!method.equalsIgnoreCase("DELETE"));
			connection.setDoInput(true);

			if (!method.equalsIgnoreCase("DELETE")) {
				// Send Request
				DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
				wr.write(message.getPayload());
				wr.flush();
				wr.close();
			}

			// Get Response
			InputStream is = connection.getInputStream();
			byte[] payload = IOUtils.toByteArray(is);
			is.close();

			response = new Response(payload, connection.getResponseCode(), connection.getHeaderFields());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}

		return response;
	}

	public static String normalizeUri(String uri) {
		String path = uri;
		if (path.startsWith("/")) {
			path = path.substring(1);
		}
		if (path.endsWith("/")) {
			path = path.substring(0, path.length() - 1);
		}
		return path;
	}
}
